(function () {
    /**
     * 理牌界面
     * 包含换牌，选择推送的推荐组合，特殊牌型提示
     * @class SortCardsController
     * @constructor
     */
    function SortCardsController() {
        this.SortCardUI = null;
        this.TimerSort = null;
        this.AlertSpecial = null;

        this.tableStatus = null;
        this.UserID = null;
        this.SpecialCard = {};

        viewer.register("arena.start", this.start.bind(this));
        viewer.register("arena.resize", this.resize.bind(this));
        viewer.register("room.loaded", this.init.bind(this));
        viewer.register("room::into", this.intoRoom.bind(this));
        viewer.register("room::leaveTable", this.leaveRoom.bind(this));
        viewer.register("game::dealer", this.dealCards.bind(this));
        viewer.register("game::check", this.exchangeCards.bind(this));
        // viewer.register("game::confirm", this.readyCompare.bind(this));
        viewer.register("game::over", this.reset.bind(this));
        // viewer.register("game::waiting", this.gameWaiting.bind(this));

    }
    var _proto = SortCardsController.prototype;
    /**
     * @privite
     * 绑定事件
     */
    _proto.bindEvent = function () {
        this.SortCardUI.closeHandler = new Laya.Handler(this, function (name) {
            this.TimerSort.y = 480;
            utils.log(name);
            utils.log(this.SortCardUI.CurrentCards);

            switch(name){
                case "play.normal":
                    viewer.publish("io.emit", {
                        cmd : "game::confirm",
                        params : {
                            token : token,
                            poker : this.SortCardUI.CurrentCards
                        }
                    });
                    break;
                case "play.special":
                    viewer.publish("io.emit", {
                        cmd : "game::confirm",
                        params : {
                            token : token,
                            sp : this.SortCardUI.CurrentCards
                        }
                    });
                    break;
                case "play.timeout":
                    //倒计时结束，用户未点击出牌，关闭特殊牌型弹窗
                    if(this.AlertSpecial.isPopup){
                        this.AlertSpecial.close();
                    }
                    break;
            }

        });

        this.AlertSpecial.closeHandler = new Laya.Handler(this, function (name) {
            switch(name){
                case "confirm":
                    this.SortCardUI.CurrentCards = this.SpecialCard.type;
                    this.SortCardUI.close("play.special");
                    break;
                case "close":
                    viewer.publish("io.emit", {
                        cmd : "game::notUseSpecial",
                        params : {
                            token : token
                        }
                    });
                    break;
            }
        });
    }
    _proto.showSortUI = function (sortData) {
        this.SortCardUI.display(sortData);
    }
    _proto.showTimer = function (timeout, confirm) {
        timeout = timeout || 30000;
        timeout = timeout / 1000 | 0;

        if(confirm){
            this.TimerSort.y = 480;
        }else{
            this.TimerSort.y = 143;
        }
        this.TimerSort.start(timeout);

        Laya.stage.addChild(this.TimerSort);
    }
    _proto.showSpecialUI = function (sp) {
        this.SpecialCard = sp;

        if(this.SpecialCard && this.SpecialCard !== ""){
            // this.AlertSpecial.specialinfo = this.SpecialCard.name + ",3道";
            this.AlertSpecial.display(this.SpecialCard.name + ",3道");
        }
    }

    /**
     * @public
     * 接收room.loaded消息，初始化相关UI，绑定事件
     */
    _proto.init = function () {

        this.SortCardUI = new ui.Alert.SortCards();
        this.TimerSort = new ui.Com.TimerSort();
        this.AlertSpecial = new ui.Alert.Special();

        this.bindEvent();
    }
    /**
     * 接收room::leaveTable消息，用户返回大厅页
     */
    _proto.leaveRoom = function () {
        this.TimerSort.stop();
        Laya.stage.removeChild(this.TimerSort);
        this.SortCardUI.close();
        this.AlertSpecial.close();
        this.tableStatus = null;
    }
    /**
     * @public
     * 接收room::into消息，如果当前用户刷新页面又重新进入游戏，并且正在理牌状态，需要给当前用户发牌
     */
    _proto.intoRoom = function (res) {
        if(Arena.CurScene !== "room"){return;}
        var players = res.list;
        var UserID = this.UserID;
        var player = null;
        var timer = false;
        var tableStatus = this.tableStatus = res.tableStatus | 0;

        for(var i in players){
            var _player = players[i];
            var _gameStatus = _player.gameStatus | 0;

            if(_player.uid == UserID && _gameStatus === 3){
                if((_player.opstatus | 0) === 0){
                    this.showSortUI(_player.possible);
                    this.showTimer(res.timeout || 30000);
                    this.showSpecialUI(_player.sp);
                }else{
                    this.showTimer(res.timeout || 30000, true);
                }
                break;
            }
        }

    }
    /**
     * @public
     * 接收game::dealer消息，展示理牌界面
     */
    _proto.dealCards = function (res) {
        if(Arena.CurScene !== "room"){return;}

        this.showSortUI(res.possible);
        this.showTimer(res.timeout || 30000);
        this.showSpecialUI(res.sp);

        Laya.SoundManager.playSound("sound/deal.mp3");
    }
    /**
     * @public
     * 接收game::check消息，确定是否可以交换扑克牌
     * @param {Boolean} confirm 是否可以交换扑克牌
     */
    _proto.exchangeCards = function (confirm) {
        if(Arena.CurScene !== "room"){return;}
        if(confirm){
            this.SortCardUI.close("play.normal");
        }else{
            viewer.publish("alert.tip", "相公了！", 3000);
        }
    }
    /**
     * @public
     * 接收game::over消息，游戏结束，重置理牌界面相关变量。
     * 如果用户未点击出牌则自动关闭理牌界面
     */
    _proto.reset = function () {
        if(Arena.CurScene !== "room"){return;}
        this.SpecialCard = {};
        this.TimerSort.stop();
        Laya.stage.removeChild(this.TimerSort);

        if(this.SortCardUI.isPopup){
            this.SortCardUI.close("play.timeout");
        }
    }
    /**
     * @public
     * 游戏开始时监听arena.start消息，初始化相关变量
     */
    _proto.start = function () {
        this.UserID = Arena.UserID;
    }
    /**
     * @public
     * 监听arena.resize消息，重置界面位置
     */
    _proto.resize = function () {
        if(this.SortCardUI){
            this.SortCardUI.width = Laya.stage.width;
        }
    }

    new SortCardsController();
})();